package com.bac.rentmap.ad.publisher;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.bac.rentmap.ad.flat.jdo.JdoFlatAdDao;
import com.bac.rentmap.jdo.JdoSupport;

/**
 * Uses JDO to store ad publisher information to datastore.
 *
 * @author Andrew Osipenko
 */
public class JdoAdPublisherService extends JdoSupport implements
        AdPublisherService {
    private static final Logger log = Logger.getLogger(JdoFlatAdDao.class
            .getName());

    public int getAdCount(String phone) {
        PersistenceManager persistenceManager = getPersistenceManager();
        try {
            AdPublisher adPublisher = persistenceManager.getObjectById(
                    AdPublisher.class, phone);
            int res = adPublisher.getAdCount();
            log.fine(res + " ads registered for " + phone);
            return res;
        } catch (JDOObjectNotFoundException e) {
            return 0;
        } finally {
            persistenceManager.close();
        }
    }

    public int registerNewAd(String phone) {
        log.fine("Registering ad for " + phone);
        PersistenceManager persistenceManager = getPersistenceManager();
        try {
            AdPublisher adPublisher;
            try {
                adPublisher = persistenceManager.getObjectById(
                        AdPublisher.class, AdPublisher.generateKey(phone));
                adPublisher.setAdCount(adPublisher.getAdCount() + 1);
            } catch (JDOObjectNotFoundException e) {
                adPublisher = new AdPublisher(phone, 1);
                persistenceManager.makePersistent(adPublisher);
            }
            return adPublisher.getAdCount();
        } finally {
            persistenceManager.close();
        }
    }

    @Override
    public int unregisterAd(String phone) {
        log.fine("Unregistering ad for " + phone);
        PersistenceManager persistenceManager = getPersistenceManager();
        try {
            AdPublisher adPublisher;
            try {
                adPublisher = persistenceManager.getObjectById(
                        AdPublisher.class, AdPublisher.generateKey(phone));
                if (adPublisher.getAdCount() <= 1) {
                    persistenceManager.deletePersistent(adPublisher);
                } else {
                    adPublisher.setAdCount(adPublisher.getAdCount() - 1);
                    return adPublisher.getAdCount();
                }
            } catch (JDOObjectNotFoundException e) {
            }
            return 0;
        } finally {
            persistenceManager.close();
        }
    }

    public void clear() {
        log.info("About to clear AdPublishers");
        PersistenceManager persistenceManager = getPersistenceManager();
        try {
            List<?> list = (List<?>) persistenceManager.newQuery(
                    AdPublisher.class).execute();
            log.info("Clearing " + list.size() + " AdPublishers");
            persistenceManager.deletePersistentAll(list);
        } finally {
            persistenceManager.close();
        }
    }
}
